package usecase

import (
	"clean/domain"
	"clean/infra/log"
	"clean/usecase/io/pb"
	"time"
)

func (a *App) Hello(in pb.Input, out *pb.Output) {
	log.Debug("app hello")
	hello := in.GetInputHello()
	if hello.Name == "" {
		out.Error = &pb.Error{
			Err: 1,
			Msg: "name not null",
		}
		return
	}
	b, err := a.SensitiveWords.IsSensitiveWords(hello.Name)
	if err != nil {
		out.Error = &pb.Error{
			Err: 1,
			Msg: err.Error(),
		}
		return
	}
	if b {
		out.Error = &pb.Error{
			Err: 1,
			Msg: "敏感词",
		}
		return
	}
	helloDomain, err := a.HelloRepo.GetByName(hello.Name)
	if err == nil {
		fillOutputHello(out, helloDomain.Msg)
		return
	}
	time.Sleep(time.Second * 2)
	msg := "welcome " + hello.Name
	a.HelloRepo.Save(&domain.Hello{
		Name: hello.Name,
		Msg: msg,
	})
	fillOutputHello(out, msg)
}
